package com.roger.springcloudGreenwich.oauth.service.impl;

import com.roger.springcloudGreenwich.oauth.bean.SecurityUser;
import com.roger.springcloudGreenwich.oauth.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl {

    @Autowired
    private UsersMapper usersMapper;

    public SecurityUser loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("执行了UserDetailsServiceImpl.loadUserByUsername==============username:==="+ username);
        SecurityUser user = usersMapper.selectUserByUserName(username);
        if(user == null){
            throw new UsernameNotFoundException("认证失败");
        }

        //查询用户所具有的角色
        user.setRoles(usersMapper.selectUserRolesByUserId(user.getId()));

        return user;
    }
}
